package com.gzyj.jtt808.v2013.model.position;

import cn.hutool.core.collection.CollectionUtil;
import com.gzyj.jtt808.utils.HexBytesUtil;
import com.gzyj.jtt808.utils.IntUtils;
import com.gzyj.jtt808.v2013.enums.AlarmFlag;

import io.netty.buffer.ByteBuf;
import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author lhj
 */
@Data
public class GnssDataBasicInfo implements Serializable {
    private List<AlarmFlag> ALARMFLAG;
    private Integer alarmFlagVal;
    private Integer STATUSBIT;
    private int LAT;//纬度
    private int LNG;//经度
    private short ALTITUDE;//海拔
    private short SPEED;//速度
    private Integer DIRECTION;//方向
    private String TIME;//时间


    private final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public int datalen() {
        return 28;
    }

    public void encode(ByteBuf buf) {
        if (CollectionUtil.isNotEmpty(this.ALARMFLAG)) {
            int sum = 0;
            for (AlarmFlag flag : ALARMFLAG) {
                sum += flag.getCode();
            }
            buf.writeInt(sum);
            this.alarmFlagVal = sum;

        } else {
            buf.writeInt(this.alarmFlagVal);

            this.ALARMFLAG = new ArrayList<>();
            String str = IntUtils.intToBinary(this.alarmFlagVal, 32);
            for (int i = 0; i < str.length(); i++) {
                char c = str.charAt(i);
                if (c == '1') {
                    int res = new Double(Math.pow(2, (32 - i - 1))).intValue();
                    ALARMFLAG.add(AlarmFlag.find(res));
                }
            }


        }
        buf.writeInt(this.STATUSBIT);


        buf.writeInt(this.LAT);
        buf.writeInt(this.LNG);
        buf.writeShort(this.ALTITUDE);
        buf.writeShort(this.SPEED);
        buf.writeShort(this.DIRECTION);

        try {
            Date date = sdf.parse(this.TIME);
            byte[] temp = HexBytesUtil.WriteDateTime_yyMMddHHmmss(date);
            buf.writeBytes(temp);

        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

    }

    public Date getGPSTimeExt() {
        try {
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return sdf.parse(this.getTIME());
        } catch (Exception e) {
            System.out.println("时间报错："+this.getTIME());
            throw new RuntimeException(e);
        }
    }

    public Double getLngExt() {
        System.out.println((double) this.getLNG()/1000000);
        return new BigDecimal((double) this.getLNG() / 1000000).setScale(6, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    public Double getLatExt() {
        return new BigDecimal((double) this.getLAT() / 1000000).setScale(6, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    public Integer getSpeedExt() {
        return this.getSPEED() / 10;
    }

    public GnssDataBasicInfo decode(ByteBuf buf) {
        byte[] temp = HexBytesUtil.readByOffset(buf, 4);
        this.alarmFlagVal = HexBytesUtil.byteArrayToInt(temp);

        this.ALARMFLAG = new ArrayList<>();
        String str = IntUtils.intToBinary(HexBytesUtil.byteArrayToInt(temp), 32);

        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (c == '1') {
                int res = new Double(Math.pow(2, (32 - i - 1))).intValue();
                ALARMFLAG.add(AlarmFlag.find(res));
            }
        }
        temp = HexBytesUtil.readByOffset(buf, 4);
        this.STATUSBIT = HexBytesUtil.byteArrayToInt(temp);

        temp = HexBytesUtil.readByOffset(buf, 4);
        this.LAT = HexBytesUtil.byteArrayToInt(temp);
        temp = HexBytesUtil.readByOffset(buf, 4);
        this.LNG = HexBytesUtil.byteArrayToInt(temp);
        temp = HexBytesUtil.readByOffset(buf, 2);
        this.ALTITUDE = Short.parseShort(HexBytesUtil.bytesToHex(temp), 16);
        temp = HexBytesUtil.readByOffset(buf, 2);
        this.SPEED = Short.parseShort(HexBytesUtil.bytesToHex(temp), 16);

        this.DIRECTION = buf.readUnsignedShort();
        temp = HexBytesUtil.readByOffset(buf, 6);
        this.TIME = HexBytesUtil.ReadDateTime_yyMMddHHmmss(temp);

        return this;
    }
}
